//
//  DetailUIViewController.m
//  DongChiDaChiProject1
//
//  Created by lanou3g on 15/10/29.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "DetailUIViewController.h"
#import "DetailView.h"

@interface DetailUIViewController ()<UITableViewDataSource, UITableViewDelegate>

// 定义一个数组存放tableview的数据
@property (nonatomic, strong) NSMutableArray *tableViewDataArray;

@end

@implementation DetailUIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    
    // 添加bar
    [self addBar];
  
    // 解析tableview的数据
    [self separateTabelViewData];
}

// 解析tableview的数据
- (void)separateTabelViewData
{
    self.tableViewDataArray = [NSMutableArray array];
    NSArray *maoHaoArray = [self.meiZhouModel.yuanliao componentsSeparatedByString:@"；"];
//    for (int i = 0; i < maoHaoArray.count; i++) {
//        // 得到每一个字符串
//        NSString *tempString = maoHaoArray[i];
//        for (int i = 0; i < tempString.length; i++) {
//            NSString *tempChar = [tempString substringWithRange:NSMakeRange(i, 1)];
//            if (tempChar isEqualToString:<#(nonnull NSString *)#>) {
//                <#statements#>
//            }
//        }
//    }
    for (NSString *tempString in maoHaoArray) {
        [self.tableViewDataArray addObject:tempString];
    }
    
    // 添加视图
    [self addView];
    
}

#pragma mark - 添加视图
- (void)addView
{
    DetailView *detailView = [[DetailView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    detailView.yuanLiaoTabel.dataSource = self;
    detailView.yuanLiaoTabel.delegate = self;
    [detailView.bigPictureImage sd_setImageWithURL:[NSURL URLWithString:self.meiZhouModel.thumb_2] placeholderImage:nil];
    [detailView.smallPictureImage sd_setImageWithURL:[NSURL URLWithString:self.meiZhouModel.thumb] placeholderImage:nil];
    detailView.titleLabel.text = self.meiZhouModel.title;
    detailView.effectLabel.text = self.meiZhouModel.effect;
    detailView.shiYongRenQunLabel.text = self.meiZhouModel.age;
//    NSString *newYuanLiaoString = [self.meiZhouModel.yuanliao stringByReplacingOccurrencesOfString:@"；" withString:@";"];
//    NSString *newYuanLiaoString1 = [newYuanLiaoString stringByReplacingOccurrencesOfString:@";" withString:@"\n"];
// //   NSLog(@"newYuanLiaoString = %@", newYuanLiaoString);
//    
//    detailView.yuanLiaoLabel.text = newYuanLiaoString1;
    detailView.yingYangLabel.text = self.meiZhouModel.yingyang;
    // 适应高度的方法
    [detailView changeFrameOfScrollViewWithShiCai:self.tableViewDataArray.count yingYang:self.meiZhouModel.yingyang];
    
    // 添加喜欢的方法
    [detailView.xinXingButton addTarget:self action:@selector(actionToCollect:) forControlEvents:UIControlEventTouchUpInside];
    
    
    detailView.backgroundColor = [UIColor brownColor];
    [self.view addSubview:detailView];
}

#pragma mark - 收藏的方法
- (void)actionToCollect:(UIButton *)button
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"该界面目前只支持浏览哦！您可在全部分类界面收藏您想要的菜品" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [alertView show];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alertView dismissWithClickedButtonIndex:0 animated:YES];
    });
    NSLog(@"收藏");
}

#pragma mark - tabelView必须实现的2个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableViewDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"456";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    if (self.tableViewDataArray.count > indexPath.row) {
        cell.textLabel.text = self.tableViewDataArray[indexPath.row];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44 * kWidthFitParameter;
}


#pragma mark - 添加bar
- (void)addBar
{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(returnToLast:)];
    leftItem.tintColor = [UIColor colorWithHue:0.8 saturation:1 brightness:0.7 alpha:1];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    [self.navigationController.navigationBar setTranslucent:NO];
}

- (void)returnToLast:(UIBarButtonItem *)barItem
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
